package com.taurus.isms;


import java.util.HashMap;
import java.util.Map;

import com.taurus.isms.ThreadsFragment.OnThreadsItemClickListener;
import com.taurus.isms.threads.MmsMedia;
import com.taurus.isms.threads.MmsMediaCache;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;



public class MainActivity extends FragmentActivity{

	
	private static final String TAG="MainActivity";
	private float mDensity;
	private int mScreenWidth;
	private int mScreenHeight;
	
	//topBar==========================
	private RelativeLayout mTopBar;
	private TextView mTopBarTitleView;
	private ImageView mTopBarBackIconView;
	private TextView mTopBarEditButton;
	private ImageView mTopBarNewSmsButton;
	private TextView mTopBarContactName;
	private TextView mTopBarContactButton;
	
	//Fragment========================
	private View mFragmentContainer1;
	private View mFragmentContainer2;
	private FragmentManager mFragmentManager;
	private ThreadsFragment mThreadsFragment;
	private ChatFragment mChatFragment;
	private View mChatFragmentTopBar;
	private TextView mChatFragmentTopBarCancelButton;
	//Fragment����=======================
	AnimatorSet mChatFragmentShowAnimator;
	AnimatorSet mChatFragmentHideAnimator;
	AnimatorSet mChatFragmentInFromBottomAnimator;
	AnimatorSet mChatFragmentOutToBottomAnimator;
	//ThreadFragment
	private long mSelectedThreadId=-1;
	
	//״̬
	private int mStatus;
	private static final int STATUS_THREAD_SHOWED = 0;
	private static final int STATUS_CHAT_SHOWED=1;
	private static final int STATUS_NEW_SMS_SHOWED=2;
	private static final int STATUS_THREAD_LIST_EDIT =3;
	
	private MmsMediaCache mMmsMediaCache;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.d("MainActivity", "onCreate..........");
		mMmsMediaCache = new MmsMediaCache(this);
		//ϵͳ����
		mDensity = getResources().getDisplayMetrics().density;
		DisplayMetrics displayMetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
		mScreenWidth = displayMetrics.widthPixels;
		mScreenHeight = displayMetrics.heightPixels;
		
		
		//topBar==========================
		mTopBar = (RelativeLayout)findViewById(R.id.mainTopBar);
		mTopBarTitleView = (TextView)findViewById(R.id.mainTopBarTitle);
		mTopBarBackIconView = (ImageView) findViewById(R.id.mainTopBarBackIcon);
		mTopBarEditButton = (TextView) findViewById(R.id.mainTopBarEditButton);
		mTopBarNewSmsButton = (ImageView) findViewById(R.id.mainTopBarNewSmsButton1);
		mTopBarContactName = (TextView) findViewById(R.id.mainTopBarContactName);
		mTopBarContactButton = (TextView) findViewById(R.id.mainTopBarContactButton);
		initTopBar();

		
		//Fragment==============================
		mFragmentContainer1 = (View) findViewById(R.id.mainFragmentContainer1);
		mFragmentContainer2 = (View) findViewById(R.id.mainFragmentContainer2);
		mFragmentContainer2.setVisibility(View.GONE);;
		
		mFragmentManager= getSupportFragmentManager();
		FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
		
		mThreadsFragment = new ThreadsFragment();
		mThreadsFragment.setOnThreadsItemClickListener(mOnThreadsItemClickListener);
		mChatFragment = new ChatFragment();
		fragmentTransaction.add(R.id.mainFragmentContainer1,mThreadsFragment);
		fragmentTransaction.add(R.id.mainChatFragmentContainer,mChatFragment);
		fragmentTransaction.commit();
		//fragment topbar
		mChatFragmentTopBar = findViewById(R.id.mainFragmentTopBar);
		mChatFragmentTopBarCancelButton = (TextView) findViewById(R.id.mainChatFragmentTopBarCancelButton);
		initChatFragmentTopBar();
		//Fragment End========================
		
		
	}


	OnThreadsItemClickListener mOnThreadsItemClickListener = new OnThreadsItemClickListener(){

		@Override
		public void onClick(View v, long threadId, long smsId,
				CharSequence contactName) {
			// TODO Auto-generated method stub
			Log.d(TAG,"onThreadItemClick");
			mFragmentContainer2.setVisibility(View.VISIBLE);
			if(mChatFragmentShowAnimator ==null){
				initFragmentAnimator();
			}
			mSelectedThreadId=threadId;
			mStatus=STATUS_CHAT_SHOWED;
			mChatFragment.initChatView(ChatFragment.SHOW_FLAG_CHAT);
			mTopBarEditButton.setVisibility(View.GONE);
			mTopBarContactName.setText(contactName);		
			mTopBarTitleView.setTextColor(getResources().getColor(R.color.iosBlue));
			mChatFragmentShowAnimator.start();
			
		}
		
	};
	

		
	public void initFragmentAnimator(){
		
		int backIconRight =mTopBarBackIconView.getRight();
		int mTopBarTitleViewWidth = mTopBarTitleView.getWidth();
		
		ObjectAnimator soa1 = ObjectAnimator.ofFloat(mFragmentContainer2,"translationX", mScreenWidth,0);
		ObjectAnimator soa2 = ObjectAnimator.ofFloat(mTopBarTitleView, "translationX", (mScreenWidth-mTopBarTitleViewWidth)/2,backIconRight-10*mDensity);
		ObjectAnimator soa3 = ObjectAnimator.ofFloat(mTopBarContactName, "translationX", mScreenWidth/2,0);
		ObjectAnimator soa4 = ObjectAnimator.ofFloat(mTopBarBackIconView, "alpha", 0.0f,1.0f);
		ObjectAnimator soa5 = ObjectAnimator.ofFloat(mTopBarTitleView, "alpha",  1.0f,0.0f,1.0f);
		ObjectAnimator soa6 = ObjectAnimator.ofFloat(mTopBarContactName, "alpha", 0.0f,1.0f);
		ObjectAnimator soa7 = ObjectAnimator.ofFloat(mTopBarContactButton, "alpha", 0.0f,1.0f);
		ObjectAnimator soa8 = ObjectAnimator.ofFloat(mTopBarNewSmsButton, "alpha", 1.0f,0.0f);
		ObjectAnimator soa9 = ObjectAnimator.ofFloat(mTopBarEditButton, "alpha", 1.0f,0.0f);
		
		
		mChatFragmentShowAnimator = new AnimatorSet();
		mChatFragmentShowAnimator.setDuration(400);
		mChatFragmentShowAnimator.playTogether(soa1,soa2,soa3,soa4,soa5,soa6,soa8);
		mChatFragmentShowAnimator.play(soa7).after(300);
		mChatFragmentShowAnimator.play(soa9).after(300);
		 
		
		ObjectAnimator hoa1 = ObjectAnimator.ofFloat(mFragmentContainer2,"translationX", 0,mScreenWidth);
		ObjectAnimator hoa2 = ObjectAnimator.ofFloat(mTopBarTitleView, "translationX", backIconRight-10*mDensity, (mScreenWidth-mTopBarTitleViewWidth)/2);
		ObjectAnimator hoa3 = ObjectAnimator.ofFloat(mTopBarContactName, "translationX", 0, mScreenWidth/2);
		ObjectAnimator hoa4 = ObjectAnimator.ofFloat(mTopBarBackIconView, "alpha", 1.0f,0.0f);
		ObjectAnimator hoa5 = ObjectAnimator.ofFloat(mTopBarTitleView, "alpha",  1.0f,0.0f,10f);
		ObjectAnimator hoa6 = ObjectAnimator.ofFloat(mTopBarContactName, "alpha", 1.0f,0.0f);
		ObjectAnimator hoa7 = ObjectAnimator.ofFloat(mTopBarContactButton, "alpha", 01.0f,0.0f);
		ObjectAnimator hoa8 = ObjectAnimator.ofFloat(mTopBarNewSmsButton, "alpha", 0.0f,1.0f);
		ObjectAnimator hoa9 = ObjectAnimator.ofFloat(mTopBarEditButton, "alpha", 0.0f,1.0f);
		mChatFragmentHideAnimator = new AnimatorSet();
		mChatFragmentHideAnimator.setDuration(400);
		mChatFragmentHideAnimator.playTogether(hoa1,hoa2,hoa3,hoa4,hoa5,hoa6,hoa7);
		mChatFragmentHideAnimator.play(hoa8).after(300);
		mChatFragmentHideAnimator.play(hoa9).after(300);
		
		
		mChatFragmentShowAnimator.addListener(new Animator.AnimatorListener() {
			
			@Override
			public void onAnimationStart(Animator animation) {
				// TODO Auto-generated method stub
				
				mTopBarContactButton.setVisibility(View.VISIBLE);
				mChatFragmentTopBar.setVisibility(View.INVISIBLE);
				mFragmentContainer2.setVisibility(View.VISIBLE);
				mFragmentContainer2.setTranslationX(mScreenWidth);
				mFragmentContainer2.setTranslationY(0);
				mTopBarBackIconView.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onAnimationRepeat(Animator animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animator animation) {
				// TODO Auto-generated method stub
				mTopBarEditButton.setVisibility(View.GONE);
				mChatFragment.setChatView(mSelectedThreadId);
				mTopBarNewSmsButton.setVisibility(View.GONE);
				
			}
			
			@Override
			public void onAnimationCancel(Animator animation) {
				// TODO Auto-generated method stub
				
			}
		});
		
		mChatFragmentHideAnimator.addListener(new Animator.AnimatorListener() {
			
			@Override
			public void onAnimationStart(Animator animation) {
				// TODO Auto-generated method stub
				mTopBarEditButton.setVisibility(View.VISIBLE);
				mTopBarNewSmsButton.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onAnimationRepeat(Animator animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animator animation) {
				mTopBarContactButton.setVisibility(View.GONE);
				mChatFragment.resetChatView();
				mTopBarBackIconView.setVisibility(View.GONE);
			}
			
			@Override
			public void onAnimationCancel(Animator animation) {
				// TODO Auto-generated method stub
				
			}
		});
		
		
	}
	
	private void getChatFragmentInFromBottomAnimator(){
		
		ObjectAnimator ioa1 = ObjectAnimator.ofFloat(mFragmentContainer2,"translationY",mScreenHeight,0);
		ioa1.addListener(new Animator.AnimatorListener() {
			
			@Override
			public void onAnimationStart(Animator animation) {
				// TODO Auto-generated method stub
				mFragmentContainer2.setVisibility(View.VISIBLE);
				mChatFragmentTopBar.setVisibility(View.VISIBLE);
				
				mFragmentContainer2.setTranslationX(0);
				mFragmentContainer2.setTranslationY(mScreenHeight);
			}
			
			@Override
			public void onAnimationRepeat(Animator animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animator animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationCancel(Animator animation) {
				// TODO Auto-generated method stub
				
			}
		});
		mChatFragmentInFromBottomAnimator = new AnimatorSet();
		mChatFragmentInFromBottomAnimator.play(ioa1);
		
		ObjectAnimator ooa1 = ObjectAnimator.ofFloat(mFragmentContainer2,"translationY",0,mScreenHeight);
		mChatFragmentOutToBottomAnimator = new AnimatorSet();
		mChatFragmentOutToBottomAnimator.play(ooa1);
	}
	
	private void initTopBar(){
		mTopBarTitleView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		int mTopBarTitleViewWidth = mTopBarTitleView.getMeasuredHeight();
		mTopBarTitleView.setTranslationX((mScreenWidth-mTopBarTitleViewWidth)/2);
		
		//mTopBarTitleView�ĵ㰴�¼�
		mTopBarTitleView.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if(mStatus!=STATUS_CHAT_SHOWED){
					return false;
				}
				switch(event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					mTopBarBackIconView.setAlpha(0.7f);
					mTopBarTitleView.setAlpha(0.7f);
					break;
				case MotionEvent.ACTION_UP:
				case MotionEvent.ACTION_CANCEL:
					mTopBarBackIconView.setAlpha(1.0f);
					mTopBarTitleView.setAlpha(1.0f);
				break;
				default:
					break;
				}
				return false;
			}
		});
		mTopBarTitleView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mStatus!=STATUS_CHAT_SHOWED){
					return;
				}
				mStatus=STATUS_THREAD_SHOWED;
				mTopBarEditButton.setVisibility(View.VISIBLE);
				mTopBarTitleView.setTextColor(0xff000000);
				mChatFragmentHideAnimator.start();
				
			}
		});
		
		
		mTopBarNewSmsButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.d(TAG, "mTopBarNewSmsButton");
				if(mChatFragmentInFromBottomAnimator==null){
					getChatFragmentInFromBottomAnimator();
				}
				
				mChatFragment.initChatView(ChatFragment.SHOW_FLAG_NEW_SMS);
				mChatFragmentInFromBottomAnimator.start();
			}
		});
		
		
		mTopBarEditButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(mStatus ==STATUS_THREAD_LIST_EDIT){
					mTopBarEditButton.setText("�༭");
					mTopBarNewSmsButton.setVisibility(View.VISIBLE);
					mStatus= STATUS_THREAD_SHOWED;
					ObjectAnimator.ofFloat(mTopBarEditButton, "alpha", 0.0f,1.0f).start();
					mThreadsFragment.changeListEditState(true);
				}else if(mStatus == STATUS_THREAD_SHOWED){
					mTopBarEditButton.setText("���");
					mTopBarNewSmsButton.setVisibility(View.GONE);
					mStatus= STATUS_THREAD_LIST_EDIT;
					ObjectAnimator.ofFloat(mTopBarEditButton, "alpha", 0.0f,1.0f).start();
					mThreadsFragment.changeListEditState(true);
					
				}
				
				
			}
		});
		
	}
	
	
	private void initChatFragmentTopBar(){
		mChatFragmentTopBarCancelButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				if(mChatFragmentOutToBottomAnimator == null){
					getChatFragmentInFromBottomAnimator();
				}
				mChatFragmentOutToBottomAnimator.start();
			}
		});
	}
}
